import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-03-31
 * Time: 10:20
 */
public class Test {

    public static void main(String[] args) {
        // 创建1个锁对象的写法？
        Object lock = new Object();



    }

    // 思路：首先让B、C线程wait，打印A，然后唤醒B，wait A，B被唤醒后，打印B，唤醒C ，C被唤醒后，打印C ，唤醒A，如此循环10次。
    public static void main5(String[] args) {
        // 设置3个锁对象
        Object object = new Object();
        Object object2 = new Object();
        Object object3 = new Object();
        Thread A = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                // 先打印 A
                System.out.print("A");
                // 唤醒 B
                synchronized (object2) {
                    object2.notify();
                }
                // 阻塞 A
                synchronized (object) {
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        });

        Thread B = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (object2) {
                    try {
                        object2.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 打印 B
                    System.out.print("B");
                }
                // 唤醒 C
                synchronized (object3) {
                    object3.notify();
                }
            }
        });

        Thread C = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized (object3) {
                    try {
                        object3.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 打印 C
                    System.out.println("C");
                }
                // 唤醒 A
                synchronized (object) {
                    object.notify();
                }
            }
        });

        A.start();
        B.start();
        C.start();
    }

    public static void main4(String[] args) {
        // 按顺序编译，：不能在t3中调用t2，能在t2中调用t3
        Thread t3 = new Thread(()->{
            System.out.println(Thread.currentThread().getName());
        },"c");

        Thread t2 = new Thread(()->{
            try {
                t3.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        },"b");

        Thread t1 = new Thread(()->{
            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName());
        },"a");

        t1.start();
        t2.start();
        t3.start();
    }

    public static int count = 0;
    synchronized public static void addCount() {
        count++;
    }
    public static void main3(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                addCount();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                addCount();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(count);
    }

    private static int odd = 0;
    public static int even = 0;
    public static void main2(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        Random random = new Random();
        int[] array = new int[10000000];
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100)+1;
        }


        Thread t1 = new Thread(()->{
            for (int i = 0; i < array.length; i+=2) {
                even+=array[i];
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 1; i < array.length; i+=2) {
                odd+=array[i];
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        int total = odd+even;
        System.out.println(total);
        long end = System.currentTimeMillis();
        System.out.println(end-start);

    }

    public static void main1(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            System.out.println(0);
        });
        Thread t2 = new Thread(()->{
            System.out.println(1);
        });
        Thread t3 = new Thread(()->{
            System.out.println(2);
        });
        Thread t4 = new Thread(()->{
            System.out.println(3);
        });
        Thread t5 = new Thread(()->{
            System.out.println(4);
        });
        Thread t6 = new Thread(()->{
            System.out.println(5);
        });
        Thread t7 = new Thread(()->{
            System.out.println(6);
        });
        Thread t8 = new Thread(()->{
            System.out.println(7);
        });
        Thread t9 = new Thread(()->{
            System.out.println(8);
        });
        Thread t10 = new Thread(()->{
            System.out.println(9);
        });
        Thread t11 = new Thread(()->{
            System.out.println(10);
        });
        Thread t12 = new Thread(()->{
            System.out.println(11);
        });
        Thread t13 = new Thread(()->{
            System.out.println(12);
        });
        Thread t14 = new Thread(()->{
            System.out.println(13);
        });
        Thread t15 = new Thread(()->{
            System.out.println(14);
        });
        Thread t16 = new Thread(()->{
            System.out.println(15);
        });
        Thread t17 = new Thread(()->{
            System.out.println(16);
        });
        Thread t18 = new Thread(()->{
            System.out.println(17);
        });
        Thread t19 = new Thread(()->{
            System.out.println(18);
        });
        Thread t20 = new Thread(()->{
            System.out.println(19);
        });
        t1.start();
        t1.join();

        t2.start();
        t2.join();

        t3.start();
        t3.join();

        t4.start();
        t4.join();

        t5.start();
        t5.join();

        t6.start();
        t6.join();

        t7.start();
        t7.join();

        t8.start();
        t8.join();

        t9.start();
        t9.join();

        t10.start();
        t10.join();

        t11.start();
        t11.join();

        t12.start();
        t12.join();

        t13.start();
        t13.join();

        t14.start();
        t14.join();

        t15.start();
        t15.join();

        t16.start();
        t16.join();

        t17.start();
        t17.join();

        t18.start();
        t18.join();

        t19.start();
        t19.join();

        t20.start();
        t20.join();
        System.out.println("ok");

    }
}
